﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Threading;

namespace FBApp
{
	/// <summary>
	/// Interaction logic for LoadingIndicator.xaml
	/// </summary>
	public partial class LoadingIndicator : UserControl
	{
        private Storyboard story = new Storyboard();
        private bool _busy = false;

		public LoadingIndicator()
		{
			this.InitializeComponent();            
		}
		
		public void start()
		{
            try
            {
                if (_busy)
                {
                    stop();
                    start();
                }
                else
                {
                    Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
                             {
                                 this.Visibility = Visibility.Visible;
                                 Storyboard story = (Storyboard)this.FindResource("animation1");
                                 story.RepeatBehavior = RepeatBehavior.Forever;
                                 story.Begin();
                                 _busy = true;
                             }
                             ));
                }                            
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot load storyboard: " + ex.Message);
            }
            
		}
		
		public void stop()
		{
            try
            {
                if (_busy)
                {
                    Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
                        {
                            this.Visibility = Visibility.Collapsed;
                            story.Stop();
                            _busy = false;
                        }
                    ));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot stop animation: " + ex.Message);
            }                        
		}

        public bool isBusy()
        {
            return _busy;
        }
	}
}